// DlgAllSetting.cpp : implementation file
//

#include "stdafx.h"
#include "CStudyChinese.h"
#include "DlgAllSetting.h"
#include "AppSetting.h"
#include "FaceManager.h"
#include "FaceHandler.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgAllSetting dialog


CDlgAllSetting::CDlgAllSetting(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgAllSetting::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgAllSetting)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgAllSetting::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgAllSetting)
	DDX_Control(pDX, IDC_BUTTON_APPLY, m_btnApply);
	DDX_Control(pDX, IDCANCEL, m_btnCancel);
	DDX_Control(pDX, IDOK, m_btnOK);
	DDX_Control(pDX, IDC_TAB1, m_tabCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgAllSetting, CDialog)
	//{{AFX_MSG_MAP(CDlgAllSetting)
	ON_WM_PAINT()
	ON_WM_MOUSEMOVE()
	ON_WM_CTLCOLOR()
	ON_BN_CLICKED(IDC_BUTTON_APPLY, OnButtonApply)
	ON_WM_HELPINFO()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgAllSetting message handlers

BOOL CDlgAllSetting::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_tabCtrl.InsertItem(0, _T("Get Word Settings"));
	m_tabCtrl.InsertItem(1, _T("System Setting"));
	m_tabCtrl.InsertItem(2,_T("Face Setting"));
	m_tabCtrl.Init();
	
	CFaceManager* p = CFaceManager::Instance();

	
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		m_tabCtrl.SetBkgndColor(p->m_bkColor);
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		m_tabCtrl.SetBkgndColor(CFaceHandler::Instance()->m_bkColor);
		break;
	}

	m_tabCtrl.EnableDraw();
	CAppTabCtrl::EnableCustomLook(TRUE,	ETC_FLAT | ETC_COLOR | ETC_SELECTION | ETC_GRADIENT	| ETC_BACKTABS );
	
	m_btnOK.SetFlat(FALSE);
	m_btnCancel.SetFlat(FALSE);
	m_btnApply.SetFlat(FALSE);
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_OUT,p->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_IN,p->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,p->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_OUT,p->m_TextColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_IN,p->m_TextColor);
		
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_OUT,p->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_IN,p->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,p->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_OUT,p->m_TextColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_IN,p->m_TextColor);

		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_OUT,p->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_IN,p->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,p->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_OUT,p->m_TextColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_IN,p->m_TextColor);
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		break;
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgAllSetting::OnOK() 
{
	m_tabCtrl.SaveUpdateSetting() ;
	
	CAppSetting::Instance()->SaveAllToDisc();	
	AfxGetMainWnd()->ShowWindow(SW_HIDE);
	AfxGetMainWnd()->ShowWindow(SW_SHOWNORMAL);
    AfxGetMainWnd()->CenterWindow();
	CDialog::OnOK();
}

void CDlgAllSetting::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
/*	CRect rect ;
	this->GetWindowRect(&rect);
	this->ScreenToClient(&rect);

	CBitmap* oldbitmap;
	CDC MemDC;
	MemDC.CreateCompatibleDC(&dc);
	oldbitmap=MemDC.SelectObject(&CFaceManager::Instance()->m_face);
	dc.BitBlt(0,0,600,400,&MemDC,0,0,SRCCOPY);
	dc.StretchBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,600,400,SRCCOPY);
	MemDC.SelectObject(oldbitmap);
*/}

void CDlgAllSetting::OnMouseMove(UINT nFlags, CPoint point) 
{
	CDialog::OnMouseMove(nFlags, point);
	PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
}

HBRUSH CDlgAllSetting::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr; 
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		pDC->SetBkColor(CFaceManager::Instance()->m_bkColor); // change the background color
		pDC->SetTextColor(CFaceManager::Instance()->m_TextColor); // change the text color
		hbr = (HBRUSH) CFaceManager::Instance()->m_brush; //  apply the brush
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		pDC->SetBkColor(CFaceHandler::Instance()->m_bkColor); // change the background color
		pDC->SetTextColor(CFaceHandler::Instance()->m_TextColor); // change the text color
		hbr = (HBRUSH) CFaceHandler::Instance()->m_brush; //  apply the brush
		break;
	}	

	return hbr; // return brush
}

void CDlgAllSetting::OnCancel() 
{
	CDialog::OnCancel();
}

void CDlgAllSetting::OnButtonApply() 
{
	m_tabCtrl.SaveUpdateSetting() ;
	m_tabCtrl.UpdateFace();
	UpdateFace();
	InvalidateRect(NULL);
	::AfxGetMainWnd()->InvalidateRect(NULL);
}

void CDlgAllSetting::UpdateFace()
{
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		m_tabCtrl.SetBkgndColor(CFaceManager::Instance()->m_bkColor);
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		m_tabCtrl.SetBkgndColor(CFaceHandler::Instance()->m_bkColor);
		break;
	}

	m_btnOK.SetFlat(FALSE);
	m_btnCancel.SetFlat(FALSE);
	m_btnApply.SetFlat(FALSE);
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceManager::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceManager::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceManager::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceManager::Instance()->m_TextColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceManager::Instance()->m_TextColor);
		
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceManager::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceManager::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceManager::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceManager::Instance()->m_TextColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceManager::Instance()->m_TextColor);

		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceManager::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceManager::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceManager::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceManager::Instance()->m_TextColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceManager::Instance()->m_TextColor);
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnOK.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnCancel.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnApply.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		break;
	}
}

BOOL CDlgAllSetting::PreTranslateMessage(MSG* pMsg) 
{
	if(pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == VK_F1)
		{
			CAppSetting::Instance()->ExecuteHelp();
			return TRUE;
		}
	}	
	return CDialog::PreTranslateMessage(pMsg);
}

BOOL CDlgAllSetting::OnHelpInfo(HELPINFO* pHelpInfo) 
{
	CAppSetting::Instance()->ExecuteHelp();
	return TRUE;	
	return CDialog::OnHelpInfo(pHelpInfo);
}
